"""ED (Effective Dose)

.. helpdoc::
Dose response analysis is largely concerned with finding a suitable concentration of agent to achieve a desired response.  This widget calculates effective dose (ED, EC, IC) concentrations for a set of response levels.
"""


"""<widgetXML>
    <name>ED (Effective Dose)</name>
    <icon>default.png</icon>
    <tags> 
        <tag>Dose Response</tag> 
    </tags>
    <screenshots></screenshots>
    <summary>Calculates the effective dose or concentration required to reach a desired response.</summary>
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
    </widgetXML>
"""

"""
<name>Effective Dose</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<RFunctions>drc:ED</RFunctions>
<tags>Dose Response</tags>
<icon>drc.PNG</icon>

"""
from OWRpy import * 
import redRGUI
import signals


class ED(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(["drc"])
        self.setRvariableNames(["ED"])
        self.data = {}
        self.RFunctionParam_object = ''
        
        """.. rrsignals::"""
        self.inputs.addInput("object", "Dose Response Model", signals.doseResponse.DoseResponseModel, self.processobject)
        
        """.. rrsignals::"""
        self.outputs.addOutput("ED Output", "Effective Dose Model",  signals.doseResponse.EffectiveDoseModel)
        
        self.standardTab = redRGUI.base.widgetBox(self.controlArea)

        """.. rrgui::"""
        self.RFunctionParamrespLev_lineEdit =  redRGUI.base.lineEdit(self.standardTab,  label = "Response Level:", text = '50', toolTip = 'The response level.  For example for ED50 use 50.\nIf you desire more than one response level separate them by commas ex: 50, 90, 95.')
        
        """.. rrgui::"""
        self.RFunctionParamci_lineEdit =  redRGUI.base.comboBox(self.standardTab,  label = "Confidence Function:", items = ['none', 'delt', 'fls', 'tfls'])
        
        """.. rrgui::"""
        self.RFunctionParamlevel_lineEdit =  redRGUI.base.lineEdit(self.standardTab,  label = "Confidence Interval:", text = '0.95', toolTip = 'The confidence interval to use with the Confidence Function.')
        
        """.. rrgui::"""
        self.RFunctionParamtype_lineEdit =  redRGUI.base.lineEdit(self.standardTab,  label = "type:", text = '')
        
        """.. rrgui::"""
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
        
        """.. rrgui::"""
        self.RoutputWindow = redRGUI.base.textEdit(self.controlArea, label = "Output Window")
    def processobject(self, data):
        
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            self.commitFunction()
    def commitFunction(self):
        if unicode(self.RFunctionParam_object) == '': 
            self.status.setStatus(0)
            return
        if unicode(self.RFunctionParamrespLev_lineEdit.text()) == '': 
            self.status.setText('Response Level Not Set')
            return
        injection = []
        if unicode(self.RFunctionParamrespLev_lineEdit.text()) != '':
            string = 'respLev=c('+unicode(self.RFunctionParamrespLev_lineEdit.text())+')'
            injection.append(string)
        if unicode(self.RFunctionParamci_lineEdit.currentText()) != 'none':
            string = 'ci=\''+unicode(self.RFunctionParamci_lineEdit.currentText())+'\''
            injection.append(string)
        if unicode(self.RFunctionParamtype_lineEdit.text()) != '':
            string = 'type=\''+unicode(self.RFunctionParamtype_lineEdit.text())+'\''
            injection.append(string)
        if unicode(self.RFunctionParamlevel_lineEdit.text()) != '':
            string = 'level='+unicode(self.RFunctionParamlevel_lineEdit.text())
            injection.append(string)
        inj = ','.join(injection)
        self.R(self.Rvariables['ED']+'<-ED(object='+unicode(self.RFunctionParam_object)+','+inj+')')
        self.R('txt<-capture.output('+self.Rvariables['ED']+')')
        self.RoutputWindow.clear()
        tmp = self.R('paste(txt, collapse ="\n")')
        self.RoutputWindow.insertPlainText(tmp)
        
        newData = signals.doseResponse.EffectiveDoseModel(self, data = self.Rvariables["ED"])
        self.rSend("ED Output", newData)
    
